package pogvue.gui;

import java.util.*;
import java.io.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import pogvue.datamodel.*;
import pogvue.gui.event.*;
import pogvue.gui.hub.*;
import pogvue.gui.menus.*;
import pogvue.io.*;
import pogvue.util.*;

public class AlignSplitPanel extends JPanel implements Printable {

  AlignmentPanel ap1;
  AlignmentPanel ap2;

  JSplitPane     jsp;


  public AlignSplitPanel(AlignmentI al,String title, double width1, double width2, GFFFragment gff) {
    setLayout(new BorderLayout());

    Controller     controller1 = new Controller();
    Controller     controller2 = new Controller();

    AlignViewport  av1        = new AlignViewport(al,false,true,true,false);

    av1.setController(controller1);


    for (int i = 0; i < al.getHeight(); i++) {
      if (al.getSequenceAt(i) instanceof SequenceFragment){
	
	((SequenceFragment)al.getSequenceAt(i)).setAlignViewport(av1);
      }
      if (al.getSequenceAt(i) instanceof GFFFragment ) {
	
	((GFFFragment)al.getSequenceAt(i)).setAlignViewport(av1);
      }
    }

    ap1 = new AlignmentPanel(av1,controller1);
        
    av1.setRenderer(new GraphRenderer());
    av1.setFont(new Font("Helvetica",Font.PLAIN,0));
    av1.setCharWidth(width1,"AlignmentPanel");
    av1.setPIDBaseline(70);
    av1.setCharHeight(10);
	
    MenuManager m = new MenuManager(this, av1, controller1);  
	
    AlignViewport av2 = new AlignViewport(al,false,true,true,false);

    ap2 = new AlignmentPanel(av2,controller1);
	
    av2.setFont(new Font("monospaced",Font.PLAIN,(int)(width2)));
    av2.setRenderer(new ConsensusRenderer());
    av2.setCharWidth(width2,"Minipog in FileMenu");
    av2.setController(controller1);
    av2.setCharHeight(10);
    av1.setMinipog(ap2);

    if (gff != null) {
      gff.setController(controller1);
      gff.setAlignViewport(av2);
    }

    jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT,ap1,ap2);
        
    jsp.setOneTouchExpandable(true);
    //jsp.setDividerLocation(al.getHeight() * av1.getCharHeight() + 50);
    jsp.setDividerLocation(400);

    // These make sure the JSPlitPane can resize the panels.
    ap1.setMinimumSize(new Dimension(0,0));
    ap2.setMinimumSize(new Dimension(0,0));
	
    jsp.setSize(1500,1000);

    // This is needed when the panel is brought up from a Choosepanel button.
    ap1.repaint();
    ap2.repaint();
	
    add("Center",jsp);
  }

  public void setAlignment(Alignment al) {
    ap1.getAlignViewport().setAlignment(al);
    ap2.getAlignViewport().setAlignment(al);

    ap1.repaint();
    ap2.repaint();

  }
  public AlignmentPanel getAlignmentPanel1() {
    return ap1;
  }
  public AlignmentPanel getAlignmentPanel2() {
    return ap2;
  }

  public int print(Graphics g,PageFormat pf, int pi) throws PrinterException {
    System.out.println("In print");
    if (pi >=1) {
      return Printable.NO_SUCH_PAGE;
    }
    paint(g);
    return Printable.PAGE_EXISTS;
  }

}



